home *** CD-ROM | disk | FTP | other *** search
/ Mac Easy 2010 May / Mac Life Ubuntu.iso / casper / filesystem.squashfs / usr / sbin / syslogd-listfiles < prev    next >
Encoding:
Text File  |  2009-01-23  |  3.6 KB  |  142 lines

  1. #! /usr/bin/perl
  2.  
  3. # Copyright (c) 1997-9,2001,3,7-8 by Martin Schulze <joey@infodrom.org>
  4.  
  5. # This program is free software; you can redistribute it and/or modify
  6. # it under the terms of the GNU General Public License as published by
  7. # the Free Software Foundation; either version 2 of the License, or
  8. # (at your option) any later version.
  9. # This program is distributed in the hope that it will be useful,
  10. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12. # GNU General Public License for more details.
  13. # You should have received a copy of the GNU General Public License
  14. # along with this program; if not, write to the Free Software
  15. # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
  16.  
  17. use strict;
  18. use warnings;
  19. use Getopt::Long;
  20.  
  21. my $conf = "/etc/syslog.conf";
  22. my $opt_daily = 1;
  23. my $opt_all = 0;
  24. my $opt_auth = 0;
  25. my $opt_ign_size = 0;
  26. my $opt_news = 0;
  27. my $opt_skip = '';
  28. my $opt_large = 1024*1024;
  29.  
  30. sub usage
  31. {
  32.     print STDERR
  33. "
  34. Debian GNU/Linux syslogd-listfiles.  Copyright (c) 1997-9,2001,3,7-8
  35. Martin Schulze.  This is free software; see the GNU General Public Licence
  36. version 2 or later for copying conditions.  There is NO warranty.
  37.  
  38. Usage: syslogd-listfiles <options>
  39. Options: -f file    specifies another syslog.conf file
  40.          -a | --all    list all files (including news)
  41.          --auth        list all files containing auth.<some prio>
  42.      --ignore-size  don't rotate files which got too large
  43.          --large nnn    define what is large in bytes (default: 1MB)
  44.          --news        include news logfiles, too
  45.          -w | --weekly    use weekly pattern instead of daily
  46.          -s pattern    skip files matching pattern
  47. ";
  48.     exit 0;
  49. }
  50.  
  51. # Test if the file was already rotated within the last n hours
  52. # with n=5
  53. #
  54. sub rotated
  55. {
  56.     my $file = shift;
  57.     my $nfile;
  58.     my $delta = 5 * 60 * 60;
  59.     my $now = time();
  60.     
  61.     # /var/log/file -> /var/log/file.0
  62.     $nfile = $file . ".0";
  63.     if (-r $nfile) {
  64.     if (($now - (stat $nfile)[9]) > $delta) {
  65.         return 0;
  66.     } else {
  67.         return 1;
  68.     }
  69.     }
  70.  
  71.     # /var/log/file -> /var/log/OLD/file.0
  72.     $nfile =~ s,(.*)/([^/]+),$1/OLD/$2,;
  73.     if (-r $nfile) {
  74.     if (($now - (stat $nfile)[9]) > $delta) {
  75.         return 0;
  76.     } else {
  77.         return 1;
  78.     }
  79.     }
  80.  
  81.     return 0;
  82. }
  83.  
  84. GetOptions('config|f=s' => \$conf,
  85.        'skip=s' => \$opt_skip,
  86.        'large=i' => \$opt_large,
  87.        'weekly' => sub { $opt_daily = 0; },
  88.        'all|a' => \$opt_all,
  89.        'auth' => \$opt_auth,
  90.        'ignore-size' => \$opt_ign_size,
  91.        'news' => \$opt_news,
  92.        'help' => \&usage) or usage();
  93.  
  94. open (C, $conf) || die "Can't open $conf, $!";
  95. my $line = '';
  96. my @lines;
  97. while (<C>) {
  98.     next if (/^(\#|$)/);
  99.     chomp;
  100.  
  101.     s/\s*(\S.*)$/$1/ if ($line);
  102.  
  103.     $line .= $_;
  104.     chop ($line) if (/\\$/);
  105.     if (!/\\$/) {
  106.     $line =~ s/\s+/\t/;
  107.     $line =~ s/\t-/\t/;
  108.     push (@lines, $line) if ($line =~ /\t\/(?!dev\/)/);
  109.     $line = "";
  110.     }
  111. }
  112. close (C);
  113.  
  114. my %output;
  115. foreach my $line (@lines) {
  116.     my ($pat,$file) = split (/\t/,$line);
  117.  
  118.     # These files are handled by news.daily from INN, so we ignore them
  119.     next if (!$opt_news &&!$opt_all && ($pat =~ /news\.(\*|crit|err|info|notice)/));
  120.  
  121.     if ($opt_all) {
  122.     $output{$file} = 1;
  123.     } elsif ($opt_auth) {
  124.     $output{$file} = 1 if ($pat =~ /auth[^\.]*\.(?!none).*/);
  125.     } else {
  126.     my $everything = ($pat =~ /\*\.\*/);
  127.     $output{$file} = 1 if (($everything && $opt_daily)
  128.                    || (!$everything && !$opt_daily && !rotated ($file))
  129.                    || (!$opt_ign_size && ((stat $file)[7] >= $opt_large) && $opt_daily)
  130.                    );
  131.     }
  132. }
  133.  
  134. foreach my $file (keys (%output)) {
  135.     my $skip = $file;
  136.     if (!length($opt_skip) ||  $skip !~ /$opt_skip/) {
  137.     printf "%s\n", $file;
  138.     }
  139. }
  140.